package template;

import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName Setting
 * @Description TODO
 * @Author leejiliang
 * @DateTime 2021/6/30 17:44
 * @Version 1.0
 */
public abstract class Setting {

	public final String getCache(String key) {
		String val = lookupLocal(key);
		if (val == null) {
			System.out.println("缓存未命中");
			val = readFromDataBase(key);
			localPut(key, val);
		} else {
			System.out.println("缓存命中");
		}
		return val;
	}

	private String readFromDataBase(String key) {
		return key;
	}

	protected abstract String lookupLocal(String key);

	protected abstract void localPut(String key, String val);
}

class MapLocalCache extends Setting{

	private Map<String, String> cache = new HashMap<>();
	@Override
	protected String lookupLocal(String key) {
		return cache.get(key);
	}

	@Override
	protected void localPut(String key, String val) {
		cache.put(key, val);
	}
}

class RedisCache extends Setting {
	private RedisCache redisCache;

	@Override
	protected String lookupLocal(String key) {
		return redisCache.getCache(key);
	}

	@Override
	protected void localPut(String key, String val) {
		redisCache.localPut(key, val);
	}
}
class TestDemo{
	public static void main(String[] args) {
		final Setting mapLocalCache = new MapLocalCache();
		mapLocalCache.getCache("test");
		mapLocalCache.getCache("test");
	}
}
